<?php

namespace app\manystore\controller\my;

use app\common\controller\ManystoreBase;
use app\common\library\Sms as Smslib;
use fast\Random;
use think\Config;
use think\Validate;

use think\Db;
use app\manystore\Model\ManystoreModel;

/**
 * 设置接口
 */
class Setting extends ManystoreBase
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    
    /**
     * 发送短信验证码
     *
     * @ApiMethod (GET)
     * @ApiTitle    发送短信验证码

     * @ApiParams (name="captcha", type="string", required=false, description="短信验证码")
     * @ApiParams (name="password", type="string", required=false, description="新登陆密码")

     * @ApiSector   设置接口
     * @ApiReturn   ({
          "code": 1,
          "msg": "获取数据成功",
          "time": "1657984618",
          "data": null
        })
     */
    public function updatePassword()
    {
        $user = $this->auth->getUserinfo();

        $mobile = $user['mobile'];
        $event = 'manystore_resetpwd';
        $captcha = $this->request->post("captcha");
        $password = $this->request->post("password");

        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('手机号不正确'));
        }

        $ret = Smslib::check($mobile, $captcha, $event);
        if ($ret) {
            Smslib::flush($mobile, $event);

            $new_password = md5(md5($password) . $user['salt']);

            ManystoreModel::where('id',$user['id'])->update(['password' => $new_password,'loginfailure' => 0,'updatetime' => time()]);

            $this->success(__('成功'));
        } else {
            $this->error(__('验证码不正确'));
        }
    }
}